﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class reportSend : System.Web.UI.Page
    {
        SendRegisterBUS srBUS = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                    LoadData();
              
            }

        }

        private int getSessionId()
        {
            if (Session["ID"].ToString() != null || Session["ID"].ToString() != "")
            {
                return int.Parse(Session["ID"].ToString());
            }
            else
            {
                return 0;
            }
        }
        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            return null;
        }
        private int getSessionDepartmentId()
        {
            if (Session["DepartmentID"].ToString() != null || Session["DepartmentID"].ToString() != "")
            {
                return int.Parse(Session["DepartmentID"].ToString());
            }
            return 0;
        }

        protected void LoadData()
        {
            try
            {
                srBUS = new SendRegisterBUS();
                string registerId = Request.QueryString["id"] + "";
                if (registerId != "")
                {
                    DataTable ReportSource = srBUS.GetReportByRegisterId(registerId);
                    if (ReportSource.Rows.Count > 0)
                    {
                        int total = 0, err = 0, open, click, notreceve, recieve, bounce;
                        int.TryParse(ReportSource.Rows[0]["TotalSend"] + "", out total);
                        int.TryParse(ReportSource.Rows[0]["TotalErr"] + "", out err);
                        int.TryParse(ReportSource.Rows[0]["TotalOpen"] + "", out open);
                        int.TryParse(ReportSource.Rows[0]["TotalClick"] + "", out click);
                        int.TryParse(ReportSource.Rows[0]["TotalNotRecieve"] + "", out notreceve);
                        int.TryParse(ReportSource.Rows[0]["TotalBounce"] + "", out bounce);
                        int.TryParse(ReportSource.Rows[0]["TotalRecieve"] + "", out recieve);

                        lblCampianName.Text = ReportSource.Rows[0]["Subject"] + "";
                        lblTotalMailSend.Text = total + "";
                        lblOpened.Text = open + "";
                        lblClick.Text = click + "";
                        lblNotOpen2.Text = recieve - open + "";

                        lblGroupEmailTo.Text = ReportSource.Rows[0]["GroupName"] + "";
                        lblDateStart.Text = ReportSource.Rows[0]["StartSend"] + "";
                        lblDateEnd.Text = ReportSource.Rows[0]["EndSend"] + "";
                        lblEmailSend.Text = ReportSource.Rows[0]["Email"] + "";

                        CreateChart(total, err + bounce, open, 1, notreceve, lblCampianName.Text);
                    }
                }
                else lblChart.Text = "";
                //LoadOpenEmail(sendId);
            }
            catch (Exception ex)
            {
                //pnError.Visible = true;
                //lblError.Text = ex.Message;
            }
        }

       

        private void CreateChart(int total, int err, int open, int NotOpen, int notRecive, string campainName)
        {
            float percentErr = 1;
            percentErr = (err * 100) / total;
            double percentSend = 100 - percentErr;
            float percentOpen = 1;
            percentOpen = (open * 100) / total;
            double percentNotOpen = percentSend - percentOpen;
            double percentNotRecev = (notRecive * 100) / total;
        //    string html = "<table>" +
        //               " <tr><td colspan='2' style='background-color:rgb(97, 88, 88); color:White;'><h2 style='font-size: 15px;font-weight:bolder;" +
        //               " vertical-align: middle; text-transform: uppercase;  margin-top: 10px;width: 100%;'>Biểu đồ thông kê chiến dịch</h2></td></tr>" +
        //                " <tr style='height:35px'>" +
        //                                   "<td style='width:35%; text-align:left;padding-left:10px;'>Tổng số mail gửi </td>" +
        //                                  " <td style='text-align:left;'> <div style='width:" + percentSend.ToString() + "%; background-color:#59D2EB; ; text-align:center;' >" +
        //                                       "<asp:Label ID='lblTotalSend' runat='server' Text='" + percentSend.ToString() + "'>" + percentSend.ToString() + "%</asp:Label></div>  </td>" +
        //                              " </tr>" +
        //                                "<tr style='height:35px'>" +
        //                                  " <td style='width:35%; text-align:left; padding-left:10px;'>Mail lỗi </td>" +
        //                                  " <td style='text-align:left'><div style='width:" + percentErr.ToString() + "%; background-color:#59D2EB; text-align:center;'>" +
        //                                       "<asp:Label ID='lblNumberErr' runat='server' Text='" + percentErr.ToString() + "% &nbsp;'>" + percentErr.ToString() + "%</asp:Label></div> </td>" +
        //                             "  </tr>" +

        //                               " <tr style='height:35px'>" +
        //                                "   <td style='width:35%; text-align:left;padding-left:10px;'>Mail đã mở: </td>" +
        //                                 "  <td style='text-align:left'><div style='width:" + percentOpen.ToString() + "%; background-color:#59D2EB;text-align:center;' >" +
        //                                     "  <asp:Label ID='lblIsOpen' runat='server' Text='&nbsp;" + percentOpen.ToString() + "%'>" + percentOpen.ToString() + "%</asp:Label></div>  </td>" +
        //                              " </tr>" +
        //                               " <tr style='height:35px'>" +
        //                                "   <td style='width:35%; text-align:left;padding-left:10px;'>Mail chưa mở : </td>" +
        //                                  " <td style='text-align:left'><div style='width:" + percentNotOpen.ToString() + "%; background-color:#59D2EB; text-align:center;' >" +
        //                                  "     <asp:Label ID='lblNotOpen' runat='server' Text='&nbsp;" + percentNotOpen.ToString() + "%'>" + percentNotOpen.ToString() + "%</asp:Label></div> </td>" +
        //                              " </tr>" +
        //                               " <tr style='height:35px'>" +
        //                                "   <td style='width:35%; text-align:left;padding-left:10px;'>Mail từ chối nhận tin: </td>" +
        //                                 "  <td style='text-align:left'><div style='width:" + percentNotRecev.ToString() + "%; background-color:#59D2EB; text-align:center;' >" +
        //                                   "    <asp:Label ID='lblNotRecived' runat='server' Text='&nbsp;" + percentNotRecev.ToString() + "%'>" + percentNotRecev.ToString() + "%</asp:Label></div> </td>" +
        //                              " </tr>" +

        //"<tr><td colspan='2'><h2  style='font-size: 15px;font-weight:bolder; vertical-align: middle; text-transform:uppercase; height:35px; text-decoration:underline; padding-top:20px; '>Chiến dịch: " + campainName + "</h2></td></tr>" +
            string html = "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='height:100%;padding:0px;border:1px solid #dedede;'>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='font-size:16px;line-height:40px;height:40px;color:#333;font-weight:bold;text-align:center;background:#f9f9f9;'>" +
                            "Biểu đồ thống kê" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='padding:10px;'>" +
                        "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Tổng số email gửi :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#109618;height:35px;line-height:35px;width: " + percentSend.ToString() + "%;border-radius:0px;'>" +
                                    percentSend.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Email lỗi :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#dc3912;height:35px;line-height:35px;width:" + percentErr.ToString() + "%;border-radius:0px;'>" +
                                    percentErr.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Email đã mở :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#3366CC;height:35px;line-height:35px;width:" + percentOpen.ToString() + "%;border-radius:0px;'>" +
                                    percentOpen.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Email chưa mở :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#FF9900;height:35px;line-height:35px;width:" + percentNotOpen.ToString() + "%;border-radius:0px;'>" +
                                    percentNotOpen.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Từ chối nhận tin :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#0099C6;height:35px;line-height:35px;width:" + percentNotRecev.ToString() + "%;border-radius:0px;'>" +
                                    percentNotRecev.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "</div>" +
            "</div>";
            lblChart.Text = html;
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<SendMailDTO> ErrorMailList(decimal id)
        {
            //string name = "", phone = "h", email = ""; int group = -1;
            EventBUS ctBUS = new EventBUS();
            DataTable dt = ctBUS.GetErrorMailList(id,2);
            List<SendMailDTO> result = new List<SendMailDTO>();
            SendMailDTO item = new SendMailDTO();
            foreach (DataRow dr in dt.Rows)
            {
                item = new SendMailDTO();
                item.ID = Convert.ToDecimal(dr["id"] + "");
                item.To = dr["TO"] + "";
                item.Message = dr["MessageId"] + "";
                item.Status = dr["Status"] + "";
                if (dr["SendTime"] + "" != "")
                    item.SendTime = Convert.ToDateTime(dr["SendTime"] + "");
                result.Add(item);
            }

            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<SendMailDTO> OpenMailList(decimal id)
        {
            //string name = "", phone = "h", email = ""; int group = -1;
            SendRegisterBUS ctBUS = new SendRegisterBUS();
            DataTable dt = ctBUS.GetOpenList(id + "");
            List<SendMailDTO> result = new List<SendMailDTO>();
            SendMailDTO item = new SendMailDTO();
            foreach (DataRow dr in dt.Rows)
            {
                item = new SendMailDTO();
                item.ID = Convert.ToDecimal(dr["id"] + "");
                item.To = dr["email"] + "";
                item.Message = dr["MessageId"] + "";
                //item.Status = dr["Status"] + "";
                if (dr["SendTime"] + "" != "")
                    item.SendTime = Convert.ToDateTime(dr["SendTime"] + "");
                if (dr["OpenDate"] + "" != "")
                    item.OpenTime = Convert.ToDateTime(dr["OpenDate"] + "");
                result.Add(item);
            }

            return result;
        }

    }
    public class SendMailDTO
    {
        public decimal ID { get; set; }
        public DateTime SendTime { get; set; }
        public DateTime OpenTime { get; set; }
        public string Status { get; set; }
        public string To { get; set; }
        public string Message { get; set; }
    }

}